package com.gitee.neuray.security.par.basicData.controller;

import com.alibaba.fastjson.JSON;
import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.util.TreeUtil;
import com.gitee.neuray.security.common.util.UUIDUtils;
import com.gitee.neuray.security.par.basicData.entity.BasicDataTree;
import com.gitee.neuray.security.par.basicData.entity.ParBasicDataEntity;
import com.gitee.neuray.security.par.basicData.biz.ParBasicDataBiz;
import com.gitee.neuray.security.par.basicData.vo.ParBasicDataPageExcelVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.common.util.ExcelUtils;
import com.gitee.neuray.security.common.util.QueryExple;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 代码生成
 *
 * @author zjt
 * @email ${email}
 * @date 2020-08-21 17:18:13
 */
@RestController
@RequestMapping("/basicData")
public class ParBasicDataController extends BaseController<ParBasicDataBiz, ParBasicDataEntity>{

    private final String ROOT = "root";

    @Autowired
    private ParBasicDataBiz parBasicDataBiz;


    /**
     * @param  basicData 参数列表
     * @return 结果集
     */
    @Override
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<ParBasicDataEntity> add(@RequestBody ParBasicDataEntity basicData) {
        //添加空字段默认值
        String id = UUIDUtils.generateShortUuid();
        basicData.setId(id);
        basicData.setParLevel(basicData.getParLevel()+1);
        baseBiz.insertSelective(basicData);
        return new ObjectRestResponse<ParBasicDataEntity>().rel(true);
    }

    /**
     * 更新方法
     * @param basicData
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    @Override
    public ObjectRestResponse<ParBasicDataEntity> update(@RequestBody ParBasicDataEntity basicData) {
        baseBiz.updateSelectiveById(basicData);
        return new ObjectRestResponse<ParBasicDataEntity>().rel(true);
    }

    /**
     * 删除方法
     * @param id
     * @return
     */
    @Override
    public ObjectRestResponse<ParBasicDataEntity> remove(@PathVariable String id) {
        // 同时删除下级的所有数据
        parBasicDataBiz.delBasicDataByParentId(id);
        return super.remove(id);
    }

    /**
     * 获取组织树
     * @param map
     * @return
     */
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public ListRestResponse<List<BasicDataTree>> tree(@RequestParam Map<String, String> map) {
        String parentTreeId = map.get("parentTreeId");
        if (StringUtils.isEmpty(parentTreeId)) {
            parentTreeId = ROOT;
        }
        List<ParBasicDataEntity> entities = parBasicDataBiz.queryAll(map);
        List<BasicDataTree> tree = getTree(entities, parentTreeId);
        return new ListRestResponse("", tree.size(), tree);
    }

    private List<BasicDataTree> getTree(List<ParBasicDataEntity> entities, String parentTreeId) {
        List<BasicDataTree> trees = new ArrayList<>();
        BasicDataTree node;
        for (ParBasicDataEntity entity : entities) {
            node = new BasicDataTree();
            String jsonNode = JSON.toJSONString(entity);
            node = JSON.parseObject(jsonNode, BasicDataTree.class);
            node.setLabel(entity.getName());
            node.setOrder(entity.getXh());
            node.setLevel(entity.getParLevel());
            node.setKpfs(entity.getKpfs());
            node.setKpbz(entity.getKpbz());
            node.setKpd(entity.getKpd());
            node.setKpdl(entity.getKpdl());
            node.setDwlx(entity.getDwlx());
            node.setDeptCode(entity.getDeptCode());
            trees.add(node);
        }
        Collections.sort(trees, Comparator.comparing(BasicDataTree::getOrder));
        return TreeUtil.bulid(trees, parentTreeId);
    }
}
